<template>
  <div style="padding: 0 50px">
    <Header class="header-cont main__title--top">{{$route.params.name}}</Header>
    <div class="col">
      <label :class="{star: !isDisabled}">所属机构</label>
      <Input v-model="data.parentName"
             class="input-1"
             :disabled='true' />
    </div>
    <div class="col">
      <label :class="{star: !isDisabled}">部门编号</label>
      <Input v-model="data.code"
             placeholder="请输入编号"
             class="input-1"
             :disabled="this.$route.params.type != 'add'"
             maxlength='30' />
    </div>
    <div class="col">
      <label :class="{star: !isDisabled}">部门名称</label>
      <Input v-model="data.name"
             placeholder="请输入部门名称"
             class="input-1"
             :disabled='isDisabled'
             maxlength='60' />
    </div>
    <div class="col">
      <label>概况说明</label>
      <Input v-model="data.description"
             maxlength="500"
             type="textarea"
             placeholder="请输入概况说明"
             :rows="4"
             style="width: 500px;height:200px"
             :disabled='isDisabled' />
    </div>
    <div class="foot-modal"
         v-show="!isDisabled">
      <Row>
        <Col span="2"
             offset="8">
        <Button type="primary"
                @click="save">保存</Button>
        </Col>
        <Col span="2">
        <Button @click='closeTab'>取消</Button>
        </Col>
      </Row>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from 'vuex'
export default {
  data() {
    return {
      typeList: [],
      proList: [],
      data: {}, //详情数据保存
      isDisabled: false
    }
  },
  methods: {
    ...mapMutations(['closeTag']),
    ...mapActions(['getNode']),
    closeTab() {
      this.closeTag({
        name: this.$route.name,
        params: this.$route.params
      })
    },
    getDetail(id) {
      if (this.$route.params.name === '日志详情') {
        this.$api.operationLog.getOperationLogDetail(id).then(res => {
          if (res.code === 200) {
            let data = JSON.parse(res.data.params)
            this.data = { ...data }
          } else {
            this.$Message.error(res.message)
          }
        })
      } else {
        this.$api.ProjectTeam.depDetail(id).then(res => {
          if (res.code == 200) {
            this.data = res.data
            this.data.parentName = this.$route.params.enterpriseName
            this.data.enterpriseId = this.$route.params.enterpriseId
            this.data.objectId = this.$route.params.objectId
          } else {
            this.$Message.error(res.message)
          }
        })
      }
    },
    save() {
      let params = this.$route.params
      if (!this.data.name) {
        this.$Message.warning('请填写部门名称')
      } else if (!this.data.code) {
        this.$Message.warning('请填写部门编号')
      } else {
        if (this.$route.params.type === 'add') {
          let reqData = {
            name: this.data.name,
            code: this.data.code,
            parentId: this.data.enterpriseId,
            parentName: this.data.parentName,
            type: this.$route.params.typeObj,
            description: this.data.description
          }
          this.$api.ProjectTeam.depAdd(reqData).then(res => {
            if (res.code === 200) {
              this.$Message.success(res.message)
               res.data.mode = 'add'
               res.data.children = []
              this.getNode(res.data)
              this.$router.push({
                name: 'project-team'
              })
              this.closeTag({
                name: 'project-team-dep-add',
                params: params
              })
            } else {
              this.$Message.warning(res.message)
            }
          })
        } else {
          let reqData = {
            id: this.data.id,
            name: this.data.name,
            code: this.data.code,
            parentId: this.data.parentId,
            parentName: this.data.parentName,
            type: this.$route.params.typeObj,
            description: this.data.description
          }
          this.$api.ProjectTeam.depEdit(reqData).then(res => {
            if (res.code === 200) {
              this.$Message.success(res.message)
               res.data.mode = 'edit'
               res.data.children = []
              this.getNode(res.data)
              this.$router.push({
                name: 'project-team'
              })
              this.closeTag({
                name: 'project-team-dep-edit',
                params: params
              })
            } else {
              this.$Message.warning(res.message)
            }
          })
        }
      }
    },
    getProList(type, id) {
      //机构编号
      this.$api.ProjectTeam.findCode(type, id, 'dep')
        .then(res => {
          if (res.code === 200) {
            this.data = {}
            this.data.code = res.data
            this.data.parentName = this.$route.params.enterpriseName
            this.data.enterpriseId = this.$route.params.enterpriseId
            this.data.objectId = this.$route.params.objectId
          } else {
            this.$Message.warning(res.message)
          }
        })
        .catch(() => {
          this.$Message.warning('生成编号失败')
        })
    }
  },
  // watch: {
  //   $route() {
  //     if (this.$route.name === 'project-team-dep') {
  //       this.isDisabled = this.$route.params.type == 'details' ? true : false
  //       if (this.$route.params.type != 'add') {
  //         this.getDetail(this.$route.params.objectId)
  //       } else {
  //         this.getProList(
  //           this.$route.params.typeObj,
  //           this.$route.params.enterpriseId
  //         )
  //       }
  //     }
  //     // window.console.log(this.$route.params)
  //     // window.console.log(this.data)
  //   }
  // },
  mounted() {
    this.isDisabled = this.$route.params.type == 'details' ? true : false
    if (this.$route.params.type != 'add') {
      this.getDetail(this.$route.params.objectId)
    } else {
      this.getProList(
        this.$route.params.typeObj,
        this.$route.params.enterpriseId
      )
    }
    // window.console.log(this.$route.params)
    // window.console.log(this.data)
  }
}
</script>

<style scoped lang="scss">
.col {
  padding: 5px;
  .col-item {
    display: inline-block;
    margin-right: 10px;
  }
  label {
    display: block;
  }
  .input-1 {
    width: 300px;
  }
  .input-2 {
    width: 150px;
  }
}
.asterisk:before {
  content: '*';
}
.header-modal {
  padding: 30px 10px;
  font-size: 16px;
  font-weight: bold;
}
.foot-modal {
  height: 100px;
  width: 100%;
  text-align: center;
  position: fix;
  bottom: 20px;
}
.star:before {
  content: '*';
  display: inline-block;
  margin-right: 4px;
  line-height: 1;
  font-family: SimSun;
  font-size: 14px;
  color: #ed4014;
}
</style>
